//
//  mm_repairCarCheckViewControllerTmp.m
//  HondaPro_2
//
//  Created by Eric on 13-7-21.
//  Copyright (c) 2013年 glendale. All rights reserved.
//

#import "mm_repairCarCheckViewControllerTmp.h"
#import "mm_userDefinedCellForCarRepairCheck.h"
#import "UIViewController+MJPopupViewController.h"
#import "MJPopupBackgroundView.h"

@interface mm_repairCarCheckViewControllerTmp ()<UITableViewDataSource,UITableViewDelegate>

/** 存放油量下拉数据 */
@property(nonatomic,strong)NSDictionary *oilDic;
@property(nonatomic,strong)NSDictionary *oilCodeDic;
@property(nonatomic,strong)NSArray *oilNamesArr;
@property (weak, nonatomic) IBOutlet UIButton *oilBtn;

/** 天窗 */
@property (weak, nonatomic) IBOutlet UIButton *windowBtn;
/** 座椅 */
@property (weak, nonatomic) IBOutlet UIButton *seatBtn;
/** 音响 */
@property (weak, nonatomic) IBOutlet UIButton *soundsBtn;
/** 空调 */
@property (weak, nonatomic) IBOutlet UIButton *airconditioningBtn;
/** 玻璃升降 */
@property (weak, nonatomic) IBOutlet UIButton *glassBtn;
/** 点烟器 */
@property (weak, nonatomic) IBOutlet UIButton *lighterBtn;
/** 防盗器 */
@property (weak, nonatomic) IBOutlet UIButton *carBurglarBtn;
/** 存放功能确认按钮对应值的集合 */
@property(nonatomic,strong) NSDictionary *funtionDicBtns;

/** 眼镜 */
@property (weak, nonatomic) IBOutlet UIButton *glassesBtn;
/** 备胎 */
@property (weak, nonatomic) IBOutlet UIButton *backupTireBtn;
/** 工具包 */
@property (weak, nonatomic) IBOutlet UIButton *toolPackageBtn;
/** 千斤顶 */
@property (weak, nonatomic) IBOutlet UIButton *jackBtn;
/** 警示牌 */
@property (weak, nonatomic) IBOutlet UIButton *warningBoardBtn;
/** 灭火器 */
@property (weak, nonatomic) IBOutlet UIButton *fireExtinguisherBtn;
/** 香烟 */
@property (weak, nonatomic) IBOutlet UIButton *cigaretteBtn;
/** 香烟说明 */
@property (weak, nonatomic) IBOutlet UITextField *cigaretteRemarkTxt;
/** 酒 */
@property (weak, nonatomic) IBOutlet UIButton *beerBtn;
/** 酒说明 */
@property (weak, nonatomic) IBOutlet UITextField *beerRemarkTxt;
/** 现金 */
@property (weak, nonatomic) IBOutlet UIButton *cashBtn;
/** 现金说明 */
@property (weak, nonatomic) IBOutlet UITextField *cashRemarkTxt;
/** 存放物品确认按钮对应值的集合 */
@property(nonatomic,strong) NSDictionary *goodsDicBtns;

@property (strong, nonatomic) IBOutlet UIButton *carRemarkImgBtn;
@property (strong, nonatomic) IBOutlet UITableView *tableViewDis;

/** 是否洗车 */
@property (strong, nonatomic) IBOutlet UIButton *cleanCarIns;

/** 在店等待 */
@property (strong, nonatomic) IBOutlet UIButton *inShopIns;

/** 是否旧件带走 */
@property (strong, nonatomic) IBOutlet UIButton *takeOldPartsIns;
/** 特别说明 */
@property (strong, nonatomic) IBOutlet UITextView *remarksTxtView;

/** 缺陷数据列表 */
@property(nonatomic,strong)NSMutableArray *defectList;


@end

@implementation mm_repairCarCheckViewControllerTmp

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        NSArray  *apparray= [[NSBundle mainBundle]loadNibNamed:@"mm_repairCarCheckViewControllerTmp" owner:nil options:nil];
        self = [apparray firstObject];
        self.tableViewDis.dataSource = self;
        self.tableViewDis.delegate =self;
    }
    return self;
}
-(void)awakeFromNib{

}

-(void)setDicRepairCarCheckData:(NSDictionary *)dicRepairCarCheckData{
    _dicRepairCarCheckData = dicRepairCarCheckData;
    
    NSArray *functionBtns = self.funtionDicBtns.allValues;
    [functionBtns makeObjectsPerformSelector:@selector(setSelected:) withObject:NO];
    
    NSArray *goodsBtns = self.goodsDicBtns.allValues;
    [goodsBtns makeObjectsPerformSelector:@selector(setSelected:) withObject:NO];
    
    self.cleanCarIns.selected = NO;
    self.inShopIns.selected = NO;
    self.takeOldPartsIns.selected = NO;
    
    [self setDataForViews];
}

-(void)setDataForViews{
    //基本信息
    NSDictionary *baseInfoDic = self.dicRepairCarCheckData[@"baseInfo"];
    
    //油量
    id code = [baseInfoDic[@"OIL_REMAIN"]stringValue];
    [self.oilBtn setTitle:self.oilCodeDic[code] forState:UIControlStateNormal];
    
    //功能确认
    [self initFunctionBtnWithStr:baseInfoDic[@"TROUBLE_CONFIRM"]];
    
    //物品确认
    [self initGoodConfirmWithArr:self.dicRepairCarCheckData[@"goodsConfirm"]];
    
    //特别说明
    self.remarksTxtView.text = baseInfoDic[@"ITEMS_SPECIAL_DESC"];
    
    //是否洗车
    code = [baseInfoDic[@"IS_WASH"]stringValue];
    if ([code isEqualToString:@"12781001"]) {
        self.cleanCarIns.selected = YES;
    }
    
    code = [baseInfoDic[@"IS_TAKE_PART_OLD"]stringValue];
    if ([code isEqualToString:@"12781001"]) {
        self.takeOldPartsIns.selected = YES;
    }
    
    code = [baseInfoDic[@"IS_CUSTOMER_IN_ASC"]stringValue];
    if ([code isEqualToString:@"12781001"]) {
        self.inShopIns.selected = YES;
    }
    
    //缺陷列表
    self.defectList = self.dicRepairCarCheckData[@"defectList"];
    [self.tableViewDis reloadData];
    //添加标记
    [self createRemarksOnOutCheckBtn];
}

-(void)layoutSubviews{
    [super layoutSubviews];
}

/** 初始化功能确认 */
-(void)initFunctionBtnWithStr:(NSString *)str{
    if (str && str.length>0) {
        NSArray *arr = [str componentsSeparatedByString:@","];
        for (NSString *val in arr) {
            UIButton *btn = self.funtionDicBtns[val];
            btn.selected = YES;
        }
    }
}

/** 功能确认按钮的初始化 */
-(void)setupFuncitonalButtons{
    self.funtionDicBtns = @{
                            @"1":self.windowBtn,
                            @"2":self.seatBtn,
                            @"3":self.soundsBtn,
                            @"4":self.airconditioningBtn,
                            @"5":self.glassBtn,
                            @"6":self.lighterBtn,
                            @"7":self.carBurglarBtn
                            };
}

/** 初始化物品确认 */
-(void)initGoodConfirmWithArr:(NSArray *)arr{
    for (NSDictionary *dic in arr) {
        NSString *name = dic[@"ITEMS_NAME"];
        UIButton *btn = self.goodsDicBtns[name];
        btn.selected = YES;
        NSString *remark = dic[@"REMARK"];
        if ([name isEqualToString:@"香烟"]) {
            self.cigaretteRemarkTxt.text = remark;
        }else if ([name isEqualToString:@"酒"]){
            self.beerRemarkTxt.text = remark;
        }else if ([name isEqualToString:@"现金"]){
            self.cashRemarkTxt.text = remark;
        }
    }
}

-(NSDictionary *)goodsDicBtns{
    _goodsDicBtns = @{
                      @"眼镜":self.glassesBtn,
                      @"备胎":self.backupTireBtn,
                      @"工具包":self.toolPackageBtn,
                      @"千斤顶":self.jackBtn,
                      @"警示牌":self.warningBoardBtn,
                      @"香烟":self.cigaretteBtn,
                      @"灭火器":self.fireExtinguisherBtn,
                      @"酒":self.beerBtn,
                      @"现金":self.cashBtn
                      };
    return _goodsDicBtns;
}


//设置按钮的状态
-(void)setUpTheBtnStateByState:(NSString *)state withBtn:(UIButton *)btn
{
    if ([state isEqualToString:MM_CAR_CHECK_STATE_OK_KEY_STR_])
    {
        [btn setBackgroundImage:[UIImage imageNamed:MM_RED_PNG_] forState:UIControlStateNormal];
    }
    else if ([state isEqualToString:MM_CAR_CHECK_STATE_NOT_OK_KEY_STR_])
    {
        [btn setBackgroundImage:[UIImage imageNamed:MM_GRAY_PNG_] forState:UIControlStateNormal];
    }
}

#pragma mark - tableView DataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4+self.defectList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    mm_userDefinedCellForCarRepairCheck *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (nil == cell)
    {
        cell = [[mm_userDefinedCellForCarRepairCheck alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    //cell的风格是：浅浅灰色和浅灰色间隔出现，第一行是浅灰色
    NSInteger row = [indexPath row];
    switch (row%2)
    {
        case 0:
            //浅灰色
            cell.contentView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
            break;
        case 1:
            //浅浅灰色
            cell.contentView.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
            break;
            
        default:
            break;
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (row<self.defectList.count)
    {
        for (int i = 0; i<[cell.arrForAllKeys count]; i++)
        {
            NSString *strKey = cell.arrForAllKeys[i];
            NSDictionary *data = self.defectList[row];
            id text = data[strKey];
            if ([text isKindOfClass:[NSNumber class]]) {
                text = [text stringValue];
            }
            ((UILabel *)cell.dicAllLblskOnCell[strKey]).text = text;
        }
    }
    else
    {
        for (int i = 0; i<[cell.arrForAllKeys count]; i++)
        {
            NSString *strKey = [cell.arrForAllKeys objectAtIndex:i];
            ((UILabel *)[cell.dicAllLblskOnCell objectForKey:strKey]).text = @"";
        }
    }
    
    return cell;
}

#pragma mark - tableView Delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}

//给外观检查的图片（btn）上放置标记
-(void)createRemarksOnOutCheckBtn
{
    //remove掉 btn 上所有的子视图：
    NSArray *subViewsIns = [self.carRemarkImgBtn subviews];
    for (int i = 1; i<[subViewsIns count]; i++)
    {
        [(UIView *)[subViewsIns objectAtIndex:i] removeFromSuperview];
    }
    
    for (NSDictionary *dic in self.defectList) {
        NSString *strPoint = dic[@"POINT"];
        NSRange range = [strPoint rangeOfString:@","];
        CGFloat cgPointX = [[strPoint substringToIndex:range.location] floatValue];
        CGFloat cgPointY = [[strPoint substringFromIndex:range.location+1] floatValue];
        NSString *strNumber = dic[MM_OUT_CHECK_OUT_CHECK_NUMBER_KEY_STR_];
        if (NSNotFound == range.location)
        {
            continue;
        }
        //给该坐标上添加一个编号图标
        UIImageView *imgViewIns = [[UIImageView alloc] initWithFrame:CGRectMake(cgPointX, cgPointY, 20, 20)];
        UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
        lbl.text = strNumber;
        lbl.textColor = [UIColor redColor];
        lbl.font = [UIFont systemFontOfSize:13];
        [imgViewIns addSubview:lbl];
        
        [self.carRemarkImgBtn addSubview:imgViewIns];
    }
}

-(void)createBackBtn
{
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setBackgroundColor:[UIColor clearColor]];
    backBtn.frame = CGRectMake(42, 25, 42, 40);
    [backBtn setImage:[UIImage imageNamed:@"返回.png"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:backBtn];
}

-(void)backBtnPressed:(UIButton *)sender
{
    NSArray *subViews = [self subviews];
    [[subViews objectAtIndex:[subViews count]-2] removeFromSuperview];
    [[subViews objectAtIndex:[subViews count]-1] removeFromSuperview];
}

#pragma mark - 剩余油量下拉数据
-(NSDictionary *)oilCodeDic{
    if (_oilCodeDic == nil) {
        _oilCodeDic = @{
                        @"12501001":@"0",
                        @"12501002":@"1/8",
                        @"12501003":@"2/8",
                        @"12501004":@"3/8",
                        @"12501005":@"4/8",
                        @"12501006":@"5/8",
                        @"12501007":@"6/8",
                        @"12501008":@"7/8",
                        @"12501009":@"FULL",
                        };
    }
    return _oilCodeDic;
}

-(NSDictionary *)oilDic{
    if (_oilDic == nil) {
        _oilDic = @{
                    @"0":@"12501001",
                    @"1/8":@"12501002",
                    @"2/8":@"12501003",
                    @"3/8":@"12501004",
                    @"4/8":@"12501005",
                    @"5/8":@"12501006",
                    @"6/8":@"12501007",
                    @"7/8":@"12501008",
                    @"FULL":@"12501009",
                    };
    }
    return _oilDic;
}

-(NSArray *)oilNamesArr{
    if (_oilNamesArr == nil) {
        _oilNamesArr = @[@"0",@"1/8",@"2/8",@"3/8",@"4/8",@"5/8",@"6/8",@"7/8",@"FULL"];
    }
    return _oilNamesArr;
}
@end
